package br.org.credfaz.sis.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import br.org.credfaz.sis.MysqlConnectionFactory;
import br.org.credfaz.sis.modelo.ContaCorrente;

public class ContaCorrenteDAO {

private final Connection connection;
	
	public ContaCorrenteDAO(){
		try{
			connection = new MysqlConnectionFactory().getConnection();
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void apaga(){
		String sql = "delete from contacorrente";
		PreparedStatement stmt;
		try{
			stmt = connection.prepareStatement(sql);
			
			stmt.execute();
			
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
	}
	
	public void adiciona(ContaCorrente contacorrente){
		String sql = "insert into contacorrente (numcontacorrente, dataabertura," +
				"valorsaldodispoant, valorsaldodispopatual, valorsaldodiaanterior)" +
				" values (?, ?, ?, ?, ?)";
		PreparedStatement stmt;
		try{
			stmt = connection.prepareStatement(sql);
			
			stmt.setLong(1, contacorrente.getNumcontacorrente());
			stmt.setDate(2, (Date) contacorrente.getDataabertura());
			stmt.setDouble(3, contacorrente.getValorsaldodispoant());
			stmt.setDouble(4, contacorrente.getValorsaldodispopatual());
			stmt.setDouble(5, contacorrente.getValorsaldodiaanterior());
			
			stmt.execute();
			
		}catch (SQLException e){
			throw new RuntimeException(e);
		}
	}


}
